<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="<%=basePath%>css/download.css" rel="stylesheet"
	type="text/css" />
<!-- test toolbar nav-->
<link rel="stylesheet" href="<%=basePath%>css/index.css">
<script src="<%=basePath%>js/jquery-1.11.1.min.js"
	type="text/javascript"></script>
<link rel="stylesheet" type="text/css"
	href="<%=basePath%>css/News-nav.css">
<!-- toolbar nav -->

<script language='JavaScript' type='text/javascript'
	src='<%=basePath%>js/jquery.form.js'></script>
<script language='JavaScript' type='text/javascript'
	src='<%=basePath%>js/jquery.cookie.js'></script>

</head>
<body>
	<div class="news-nav">
		<div class="container clearfix">
			<div class="nav-bar">
				<a href="<%=basePath%>download/download.jsp" target="_top"
					class="current">首页</a> <a href="<%=basePath %>download/mydata.jsp" onclick="checkLogin()">我的资源</a>
				<a href="<%=basePath%>download/search.jsp?type=3" target="_blank">搜索</a>
			</div>
		</div>
	</div>
	<!-- //toolbar nav -->
	<div id="wrap">
		<link href="<%=basePath%>css/upload.css" rel="stylesheet"
			type="text/css" />

		<!-- breadcrumb navigation -->
		<div class="way_nav">
			<a href="<%=basePath%>download/download.jsp">首页</a><em>&gt;</em>资源上传
		</div>
		<div class="content">
			<!-- left area -->
			<div class="left">
				<form id="uploadform" name="uploadform" action="<%=basePath %>UploadFileServlet"
					enctype="multipart/form-data" method="post"
					onsubmit="">
					<!-- left upload -->
					<div class="bor_box_1 upload_left">
						<table border="0" cellspacing="0" cellpadding="0" class="upload">
							<colgroup>
								<col width="110" />
								<col />
							</colgroup>
							<tr>
								<th>&nbsp;</th>
								<td>
									<dl class="note_upload">
										<dt>
											<a href="javascript:void(0);" class="btn files"> <input
												id="txt_userfile" name="txt_userfile" type="file"
												onchange="show_uploadfile();" /></a>
												<input type="hidden" value='' id="filename" name="filename" />
												<input type="hidden" value='${cookie.userId.value}'  name="userId" />
										</dt>
										<dd>
											<ul>
												<li id="li_userfile">（未选择文件）</li>
												<li>您可以上传小于<span class="red">60MB</span>的文件
												</li>
											</ul>
										</dd>
									</dl>
								</td>

							</tr>
							<tr>
								<th>资源名称：</th>
								<td><input id="txt_title" name="txt_title" type="text"
									class="input_1" /><em></em></td>
							</tr>
							<tr>
								<th>资源类型：</th>
								<td><select name="sel_filetype" id="sel_filetype">
										<option value="0">请选择</option>
										<option value="文档">文档</option>
										<option value="代码类">代码类</option>
										<option value="工具类">工具类</option>
										<option value="其他">其他</option>
								</select> <em></em></td>
							</tr>
							<tr>
								<th>关键词（Tag）：</th>
								<td><input id="txt_tag" name="txt_tag" type="text" placeholder="以空格隔开"
									class="input_1" /><em></em></td>
							</tr>

							<tr>
								<th>所属分类：</th>
								<td>
								<select id="sel_primary" name="sel_primary">
								<option value="" selected="selected">请选择</option>
								<option value="mobile_development">移动开发</option>
								<option value="development_technology">开发技术</option>
								<option value="course_resource">课程资源</option>
								<option value="network_technology">网络技术</option>
								<option value="operating_system">操作系统</option>
								<option value="security_technology">安全技术</option>
								<option value="database">数据库</option>
								<option value="industry">行业</option>
								<option value="server">服务器应用</option>
								<option value="store">存储</option>
								<option value="information">信息化</option>
								<option value="examination_authentication">考试认证</option>
								</select>
<!-- 								&nbsp; <select id="sel_subclass" style="display:none" -->
<!-- 									name="sel_subclass"><option value="" -->
<!-- 											selected="selected">请选择</option> -->
<!-- 								</select> -->
								 <em></em></td>
							</tr>
							<tr>
								<th>资源分：</th>
								<td><select name="sel_score" id="sel_score">
										<option value="0" selected="selected">0</option>
										<option value="1">1</option>
										<option value="2">2</option>
										<option value="3">3</option>
										<option value="4">4</option>
										<option value="5">5</option>
										<option value="6">6</option>
										<option value="7">7</option>
										<option value="8">8</option>
										<option value="9">9</option>
										<option value="10">10</option>
								</select> <em></em></td>
							</tr>
							<tr>
								<th valign="top">资源描述：</th>
								<td><textarea name="txt_desc" id="txt_desc" cols="" rows=""
										placeholder="描述>=20个字符,不支持HTML标签；详细的资源描述有机会获得我们的推荐，更有利于他人下载，赚取更多积分。"></textarea><em></em></td>
							</tr>
							<tr>
								<th>&nbsp;</th>
								<td><input id="btn_submit" name="" type="image"
									src="../css/images/btn_submit.png" /></td>
							</tr>
						</table>
					</div>
					<!-- upload need to know -->
					<div class="bor_box_1">
						<dl class="list_4">
							<dt>上传须知</dt>
							<dd>* 如涉及侵权内容,您的资源将被移除</dd>
							<dd>* 请勿上传小说、mp3、图片等与技术无关的内容.一旦发现将被删除</dd>
							<dd>* 请勿在未经授权的情况下上传任何涉及著作权侵权的资源，除非该资源完全由您个人创作</dd>
							<dd>* 点击上传资源即表示您确认该资源不违反资源分享的使用条款，并且您拥有该资源的所有版权或者上传资源的授权</dd>
						</dl>
					</div>
				</form>
			</div>
		</div>

		<script type="text/javascript">
		function checkLogin(){
			if($.cookie("userId")){
				return true;
			}
			if(confirm("需要登录，是否登录?")){
				var links=top.location.href.substring(7).split("/");
				top.location.href="<%=basePath%>login.jsp?link="+encodeURIComponent(links[2]+"/"+links[3]);
				return false;
			}
			return false;
		}
			function getStrLength(str) {
				var len = 0;
				for ( var i = 0; i < str.length; i++) {
					var c = str.charCodeAt(i);
					//单字节加1
					if ((c >= 0x0001 && c <= 0x007e)
							|| (0xff60 <= c && c <= 0xff9f)) {
						len++;
					} else {
						len += 2;
					}
				}
				return len;
			}
// 			function stopupload() {
// 				if (navigator.appName == "Microsoft Internet Explorer")
// 					window.document.execCommand('Stop');
// 				else
// 					window.stop();
// 			}
			var xhr;
			function show_uploadfile() {
				var filename = $('#txt_userfile').val();
				if (filename) {
					filename = filename.replace(/C:\\fakepath\\/, '');
					$('#li_userfile').html('( ' + filename + ' )');
					$("#filename").val(filename);
				}
			}
// 			function addtag(tag) {
// 				var tags = $("#txt_tag").val();
// 				var arrtags = tags.split(" ");
// 				var dtags = new Array;
// 				var j = 0;
// 				for ( var i = 0; i < arrtags.length; i++) {
// 					if (jQuery.trim(arrtags[i]) != '') {
// 						dtags[j] = arrtags[i];
// 						j++;
// 					}

// 				}
// 				for (i = 0; i < dtags.length; i++) {
// 					if (tag == dtags[i]) {
// 						return;
// 					}
// 				}
// 				if (dtags.length > 4) {
// 					alert('最多允许填写5个Tag!');
// 					return;
// 				}

// 				tags = tags + " " + tag;
// 				$("#txt_tag").val(tags);
// 			}
			
			$(document).ready(function(){
				$("#btn_submit").click(function(){
					if(validate()){
						$(this).attr("disabled","true");
						$('#uploadform').ajaxSubmit({
							success: function(data) {
								if(data=='true'){
									alert("上传成功！");
									location.href="<%=basePath%>download/mydata.jsp";
								}else{
									alert(data);
									$(this).attr("disabled","false");
								}
							}
						});

					}
					return false;
				});
			});

			function validate() {	
				var filename = $('#txt_userfile').val();
				if (!filename) {
					alert("请选择文件！");
					return false;
				}
				var isOK = false;
				if (jQuery.trim($("#txt_title").val()) == '') {
					//alert('请填写资源的标题!');
					var $em = $("#txt_title").parent().find('em');
					// $("#txt_title").parent().append( '<em class="form_error">请填写资源的标题!</em>' );
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$em.html('请填写资源的标题!');
					$("#txt_title").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}

				if (jQuery.trim($("#txt_title").val()).length > 80) {
					//alert('您的资源名称太长了!');
					var $em = $("#txt_title").parent().find('em');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$em.html('您的资源名称太长了!');
					$("#txt_title").focus();
					isOk = false
					return false;
				} else {
					isOk = true;
				}

				if (getStrLength(jQuery.trim($("#txt_title").val())) < 10) {
					//alert('您填写的资源名称长度不符合规则，请检查!');
					var $em = $("#txt_title").parent().find('em');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$em.html('您填写的资源名称长度不符合规则，请检查!');
					// $em.css( { display: 'block' } );
					$("#txt_title").focus();
					isOk = false;
					return false;
				}
				if (isOk) {
					var $em = $("#txt_title").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if ($("#sel_filetype").val() < 1) {
					//alert('请选择资源类型!');
					// $("#sel_filetype").parent().append( '<em class="form_error">请选择资源类型!</em>' );
					var $em = $("#sel_filetype").parent().find('em');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$em.html('请选择资源类型!');
					$("#sel_filetype").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				if (isOk) {
					var $em = $("#sel_filetype").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if (jQuery.trim($("#txt_tag").val()) == '') {
					//alert('请填写资源的Tag!');
					var $em = $("#txt_tag").parent().find('em');
					$em.html('请填写资源的Tag!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#txt_tag").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				if (jQuery.trim($("#txt_tag").val()).length < 2) {
					var $em = $("#txt_tag").parent().find('em');
					$em.html('资源Tag需要大于2个字符!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#txt_tag").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				var tags = jQuery.trim($("#txt_tag").val()).split(/\s+/);
				if (tags.length > 5) {
					var $em = $("#txt_tag").parent().find('em');
					$em.html('最多允许填写5个Tag!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#txt_tag").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				for ( var i = 0; i < tags.length; i++) {
					if (tags[i].replace(/[^\x00-\xff]/g, "**").length > 12) {
						// alert('您填写的'+'"'+tags[i]+'"长度超出了限制，请检查');
						$em = $("#txt_tag").parent().find('em');
						$em.html('您填写的' + '"' + tags[i] + '"长度超出了限制，请检查');
						$em.addClass('form_error');
						$em.removeClass('form_correction');
						$("#txt_tag").focus();
						isOk = false;
						return false;
					} else {
						isOk = true;
					}
				}
				if (isOk) {
					var $em = $("#txt_tag").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if (!$("#sel_primary").val()) {
					//alert('请选择分类!');
					// $("#sel_primary").parent().append( '<em class="form_error">请选择分类!</em>' );
					$em = $("#sel_primary").parent().find('em');
					$em.html('请选择分类!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#sel_primary").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				if (isOk) {
					var $em = $("#sel_primary").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if (jQuery.trim($("#sel_score").val()) > 10) {
					$em = $("#txt_tag").parent().find('em');
					$em.html('请选择正确的资源分!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#sel_score").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				if (isOk) {
					var $em = $("#sel_score").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if (jQuery.trim($("#txt_desc").val()) == '') {
					$em = $("#txt_desc").parent().find('em');
					$em.html('请填写资源描述!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#txt_desc").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				if (jQuery.trim($("#txt_desc").val()).length < 20) {
					$em = $("#txt_desc").parent().find('em');
					$em.html('资源描述可以把电子书的概述、源代码的说明、文档的片段填在这里，描述详细会获得我们的推荐，更容易被他人下载！描述大于20字不是问题吧!');
					$em.addClass('form_error');
					$em.removeClass('form_correction');
					$("#txt_desc").focus();
					isOk = false;
					return false;
				} else {
					isOk = true;
				}
				if (isOk) {
					var $em = $("#txt_desc").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if (isOk) {
					var $em = $("#cb_agree").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}

				if (isOk) {
					var $em = $("#txt_validcode").parent().find('em');
					$em.addClass('form_correction');
					$em.removeClass('form_error');
					$em.html('');
				}
				return true;
			}

		</script>
		<!-- 计数  -->
	</div>
	<link href="<%=basePath %>css/ask_float_block.css"
		rel="stylesheet" type="text/css" />
</body>
</html>